//
//  MiddleLayer.m
//  MapProject
//
//  Created by rrk on 4/22/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "OverallView.h"
#import "MiddleLayer.h"


@implementation MiddleLayer

@synthesize goalView=goalView_;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 2) {
		CGPoint corner1, corner2;
		int i = 0;
		for (UITouch *touch in touches) {
			if (i == 0)
				corner1 = [touch locationInView:self];
			else
				corner2 = [touch locationInView:self];
			i++;
		}
		CGSize size;
		size.width = (corner2.x > corner1.x) ? corner2.x - corner1.x : corner1.x - corner2.x;
		size.height = (corner2.y > corner1.y) ? corner2.y - corner1.y : corner1.y - corner2.y;
		
		CGPoint org;
		if (corner1.x < corner2.x && corner1.y < corner2.y) { //first touch is upper-left
			org.x = corner1.x;
			org.y = corner1.y;
		}
		else if (corner2.x < corner1.x && corner2.y < corner1.y) { //second touch is upper-left
			org.x = corner2.x;
			org.y = corner2.y;
		}
		else if (corner1.x > corner2.x && corner1.x < corner2.y) { // first touch is upper-right
			org.x = corner2.x;
			org.y = corner1.y;
		}
		else { // second touch is upper-right
			org.x = corner1.x;
			org.y = corner2.y;
		}
		
		CGRect newFrame;
		newFrame.origin = org;
		newFrame.size = size;
		UIView *goal = [[UIView alloc] initWithFrame:newFrame];
		[goal setBackgroundColor:[UIColor blueColor]];
		[goal setUserInteractionEnabled:NO];
		if ([self goalView] == nil) {
			[self addSubview:goal];
			[self setGoalView:goal];
		}
		else {
			[[self goalView] removeFromSuperview];
			[self addSubview:goal];
			[self setGoalView:goal];
			[self layoutSubviews];
		}
	}	
	
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 2) {
		CGPoint corner1, corner2;
		int i = 0;
		for (UITouch *touch in touches) {
			if (i == 0)
				corner1 = [touch locationInView:self];
			else
				corner2 = [touch locationInView:self];
			i++;
		}
		CGSize size;
		size.width = (corner2.x > corner1.x) ? corner2.x - corner1.x : corner1.x - corner2.x;
		size.height = (corner2.y > corner1.y) ? corner2.y - corner1.y : corner1.y - corner2.y;
		
		CGPoint org;
		if (corner1.x < corner2.x && corner1.y < corner2.y) { //first touch is upper-left
			org.x = corner1.x;
			org.y = corner1.y;
		}
		else if (corner2.x < corner1.x && corner2.y < corner1.y) { //second touch is upper-left
			org.x = corner2.x;
			org.y = corner2.y;
		}
		else if (corner1.x > corner2.x && corner1.x < corner2.y) { // first touch is upper-right
			org.x = corner2.x;
			org.y = corner1.y;
		}
		else { // second touch is upper-right
			org.x = corner1.x;
			org.y = corner2.y;
		}
		
		CGRect newFrame;
		newFrame.origin = org;
		newFrame.size = size;
		UIView *goal = [[UIView alloc] initWithFrame:newFrame];
		[goal setBackgroundColor:[UIColor blueColor]];
		[goal setUserInteractionEnabled:NO];
		if ([self goalView] == nil) {
			[self addSubview:goal];
			[self setGoalView:goal];
		}
		else {
			[[self goalView] removeFromSuperview];
			[self addSubview:goal];
			[self setGoalView:goal];
			[self layoutSubviews];
		}
	}	
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	
	NSLog(@"Ended with touches: %d", [touches count]);
	
	/* With one touch, we make a box that is 100px x 100px as a default */
	if ([touches count] == 1) {
		CGPoint center;
		for (UITouch *touch in touches)
			center = [touch locationInView:self];
		
		/* 100 x 100 px */
		CGSize size;
		size.width = 100.0f;
		size.height = 100.0f;
		
		/* Find origin */
		CGPoint org;
		org.x = (center.x - 50.0f > 0) ? (center.x - 50.0f) : 0.0;
		org.y = (center.y - 50.0f > 0) ? (center.y - 50.0f) : 0.0;
		
		/* Obviously make the bounding box */
		[self makeBoundingBoxAt:org ofSize:size];
	}
	
	/* Two touches make a box that is sized to the rectangle defined by both touches */
	if ([touches count] == 2) {
		CGPoint corner1, corner2;
		int i = 0;
		for (UITouch *touch in touches) {
			if (i == 0)
				corner1 = [touch locationInView:self];
			else
				corner2 = [touch locationInView:self];
			i++;
		}
		CGSize size;
		size.width = (corner2.x > corner1.x) ? corner2.x - corner1.x : corner1.x - corner2.x;
		size.height = (corner2.y > corner1.y) ? corner2.y - corner1.y : corner1.y - corner2.y;
		
		CGPoint org;
		if (corner1.x < corner2.x && corner1.y < corner2.y) { //first touch is upper-left
			org.x = corner1.x;
			org.y = corner1.y;
		}
		else if (corner2.x < corner1.x && corner2.y < corner1.y) { //second touch is upper-left
			org.x = corner2.x;
			org.y = corner2.y;
		}
		else if (corner1.x > corner2.x && corner1.x < corner2.y) { // first touch is upper-right
			org.x = corner2.x;
			org.y = corner1.y;
		}
		else { // second touch is upper-right
			org.x = corner1.x;
			org.y = corner2.y;
		}
		[self makeBoundingBoxAt:org	ofSize:size];
	}
}


-(void)shiftBoundingBox:(CGPoint)slideValue {
    UIView *boundingBox = [self goalView];
	[UIView beginAnimations:@"shiftBB" context:(void*)[boundingBox retain]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5f];
    
	/* Translates the right amount of pixels to center */
    CGAffineTransform transform = [boundingBox transform];
    transform = CGAffineTransformTranslate(transform, slideValue.x, slideValue.y);
    [boundingBox setTransform:transform];
    [boundingBox setAlpha:1.0f];
    
    [UIView commitAnimations];
}

-(void)zoomBoundingBox:(CGSize)scaleValues {
    UIView *boundingBox = [self goalView];
	[UIView beginAnimations:@"zoomBB" context:(void*)[boundingBox retain]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5f];
    
	/* Scales bounding box to appropriate size */
    CGAffineTransform transform = [boundingBox transform];
    transform = CGAffineTransformScale(transform, scaleValues.width, scaleValues.height);
    [boundingBox setTransform:transform];
    [boundingBox setAlpha:1.0f];
    
    [UIView commitAnimations];
}

/* Creates a bounding box according to the rectangle that will be specified by 'org' and
 'size'. This rectangle is passed to the mapView to adjust it accordingly (zoom and centering), which
 will then return what the new bounding box will look like in the zoomed in view. The bounding box
 animates with the zoom to match up to what it should look like. */
-(void)makeBoundingBoxAt:(CGPoint)org ofSize:(CGSize)size {
	CGRect newFrame;
	newFrame.origin = org;
	newFrame.size = size;
	UIView *goal = [[UIView alloc] initWithFrame:newFrame];
	[goal setBackgroundColor:[UIColor blueColor]];
	[goal setUserInteractionEnabled:NO];
	if ([self goalView] == nil) {
		[self addSubview:goal];
		[self setGoalView:goal];
	}
	else {
		[[self goalView] removeFromSuperview];
		[self addSubview:goal];
		[self setGoalView:goal];
		[self layoutSubviews];
	}
	/* Preserve old bounding box width & height */
	CGSize scaleValues;
	scaleValues.width = [self goalView].frame.size.width;
	scaleValues.height = [self goalView].frame.size.height;
	
	/* Preserve old bounding box origin */
	CGPoint slideValue;
	slideValue.x = [self goalView].frame.origin.x;
	slideValue.y = [self goalView].frame.origin.y;
	
	OverallView *parent = (OverallView*)[self superview];
	
	/* Adjust the map view to the bounding box */
	newFrame = [parent adjustMapToRect:[[self goalView] frame]];
	float start_center_x = slideValue.x + scaleValues.width/2.0;
	float start_center_y = slideValue.y + scaleValues.height/2.0;
	float end_center_x = newFrame.origin.x + newFrame.size.width/2.0;
	float end_center_y = newFrame.origin.y + newFrame.size.height/2.0;
	
	/* Create translate and scale values to animate the bounding box */
	slideValue.x = end_center_x - start_center_x;
	slideValue.y = end_center_y - start_center_y;
	scaleValues.width = newFrame.size.width / scaleValues.width;
	scaleValues.height = newFrame.size.height / scaleValues.height;
	
	/* Animate! */
	[self shiftBoundingBox:slideValue];
	[self zoomBoundingBox:scaleValues];
}

-(void)clearBoundingBox {
	if (goalView_ != nil) {
		[goalView_ removeFromSuperview];
		[goalView_ release];
		goalView_ = nil;
	}
}


@end
